package com.itheima.service.impl;

import com.itheima.entity.*;
import com.itheima.mapper.SysConfigMapper;
import com.itheima.service.SysConfigService;
import com.itheima.vo.ConfigVO;
import com.itheima.vo.PartConfigVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @projectName: kafka
 * @package: com.itheima.service.impl
 * @className: SysConfigServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2023/9/26 20:20
 * @version: 1.0
 */
@Service
@RequiredArgsConstructor
public class SysConfigServiceImpl implements SysConfigService {
    private final SysConfigMapper configMapper;

    @Override
    public ConfigNew getAllConfigurations() {
        List<Config> configList = configMapper.selectSysConfig();  // 得到全部的信息
        System.out.println("-----------------configList---------------------" + configList);

        List<ConfigData> configDataList = new ArrayList<>();
        for (Config config : configList) {              // 对全部的信息进行遍历
            String groupValue = config.getGroupValue(); // 通知公告,站点配置,站点配置,底部信息,...
            String father = config.getTabValue();       // custom,base,base,...
            int count = 0;
            for (ConfigData configData : configDataList) {
                if (configData.getName().equals(groupValue)) {   // 说明存在的
                    count++;
                }
            }
            if (count == 0) {       // 没有则添加
                ConfigData configData = new ConfigData();
                configData.setFather(father);
                configData.setName(groupValue);
                List<Config> configs = configMapper.selectByGroupValue(groupValue);
                List<ConfigVO> configVOS = new ArrayList<>();
                for (Config config1 : configs) {
                    ConfigVO configVO = new ConfigVO();
                    BeanUtils.copyProperties(config1,configVO);
                    configVO.setGroup_value(config1.getGroupValue());
                    configVO.setTab_value(config1.getTabValue());
                    configVO.setAttr_name(config1.getAttrName());
                    configVO.setAttr_value(config1.getAttrValue());
                    configVO.setDel_flag(config1.getDelFlag());
                    configVO.setConfig_value(config1.getConfigValue());
                    configVO.setCan_delete(config1.getCanDelete());
                    configVO.setSort_num(config1.getSortNum());
                    configVOS.add(configVO);
                }
                configData.setData(configVOS);
                configDataList.add(configData);
            }
        }

        List<ConfigChildren> configChildrenList = new ArrayList<>();
        for (Config config : configList) {                  // 对全部的信息进行遍历
            String tabValue = config.getTabValue();         // custom,custom,base,...
            int count = 0;
            for (ConfigChildren configChildren : configChildrenList) {
                if (configChildren.getKey().equals(tabValue)) {        // 说明存在
                    count++;
                }
            }
            if (count == 0) {  // 没有则添加
                ConfigChildren configChildren = new ConfigChildren();
                configChildren.setKey(tabValue);
                configChildren.setName(tabValue);
                configChildrenList.add(configChildren);
            }
        }
        for (int i = 0; i < configChildrenList.size(); i++) {
            List<ConfigData> configDataList1 = new ArrayList<>();
            for (ConfigData configData : configDataList) {
                if (configChildrenList.get(i).getKey().equals(configData.getFather())) {
                    ConfigData configData1 = configData;
                    configDataList1.add(configData1);
                }
            }
            configChildrenList.get(i).setChildren(configDataList1);
        }
        System.out.println("========================configChildrenList==========================" + configChildrenList);


        List<PartConfig> allData = configMapper.partialQuery();
        List<PartConfigVO> partConfigVOList = new ArrayList<>();
        for (PartConfig allDatum : allData) {
            PartConfigVO partConfigVO = new PartConfigVO();
            BeanUtils.copyProperties(allDatum,partConfigVO);
            partConfigVO.setAttr_key(allDatum.getAttrKey());
            partConfigVO.setAttr_value(allDatum.getAttrValue());
            partConfigVO.setConfig_value(allDatum.getConfigValue());
            partConfigVOList.add(partConfigVO);
        }
        System.out.println("========================partConfigVOList==========================" + partConfigVOList);

        List<AttrKey> attrKeys = configMapper.selectAttrKey();
        HashMap<String, String> map = new HashMap<>();
        for (AttrKey attrKey : attrKeys) {
            map.put(attrKey.getAttrKey(), attrKey.getAttrValue());
        }
        System.out.println("=======================map================================" + map);

        List<Validator> validatorList = configMapper.selectValidator();
        HashMap<String, String> mapValidator = new HashMap<>();
        for (int i = 0; i < validatorList.size(); i++) {
            mapValidator.put(validatorList.get(i).getAttrKey(), validatorList.get(i).getValidator());
        }
        System.out.println("=======================mapValidator================================" + mapValidator);

        ConfigNew configNew = new ConfigNew();
        configNew.setList(configChildrenList);
        configNew.setAllData(partConfigVOList);
        configNew.setConfigForm(map);
        configNew.setConfigRules(mapValidator);

        return configNew;
    }

    @Override
    public Map<String, String> getConfig() {
        List<AttrKey> attrKeys = configMapper.selectAttrKey();
        HashMap<String, String> map = new HashMap<>();
        for (AttrKey attrKey : attrKeys) {
            map.put(attrKey.getAttrKey(), attrKey.getAttrValue());
        }
        return map;
    }
}
